//用户登录信息的状态管理

import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

interface UseInfo{
    loginState:boolean
    userid:string
    token:string 
}

export const useUserstore =defineStore('user',()=>{
    
    //定义用户是否登录状态
    const loginState =ref(localStorage.getItem('loginState')=='true')

    //用户id
    const userid=ref(localStorage.getItem('userid')||'')

    //token
    const token=ref(localStorage.getItem('token')||'')


    //修改全局状态的方法
    function changeUseState (userInfo:UseInfo){
        loginState.value=userInfo.loginState
        userid.value=userInfo.userid
        token.value=userInfo.token

        //将数据放在本地存储中
        localStorage.setItem('loginState', JSON.stringify(loginState.value))
        localStorage.setItem('userid', userid.value)
        localStorage.setItem('token', token.value)
    }


    return{loginState,userid,token,changeUseState}
})